package com.example.bailin.giftsay.sort.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.bailin.giftsay.R;
import com.example.bailin.giftsay.entity.SingleEntity;
import com.example.bailin.giftsay.sort.LmClickListener;
import com.example.bailin.giftsay.tool.MyApp;
import com.example.bailin.giftsay.tool.NetTool;

/**
 * Created by bailin on 16/8/19.
 *
 * 单品页右侧每个图片item
 * 不用优化
 */
public class SingleRightRvAdapter extends RecyclerView.Adapter<SingleRightRvAdapter.RightViewHolder> {
    private SingleEntity entity;
    private int index;
    private LmClickListener lmClickListener;

    public void setLmClickListener(LmClickListener lmClickListener) {
        this.lmClickListener = lmClickListener;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public void setEntity(SingleEntity entity) {
        this.entity = entity;
    }

    @Override
    public RightViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(MyApp.getContext()).inflate(R.layout.item_rv_right, parent, false);
        RightViewHolder viewHolder = new RightViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RightViewHolder holder, final int position) {
        NetTool tool = new NetTool();
        tool.getImg(entity.getData().getCategories().get(index).getSubcategories().get(position).getIcon_url(), holder.iv);
        holder.tv.setText(entity.getData().getCategories().get(index).getSubcategories().get(position).getName());

        holder.ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                lmClickListener.onClick
                        (entity.getData().getCategories().get(index).getSubcategories().get(position).getId()
                                , entity.getData().getCategories().get(index).getSubcategories().get(position).getName());
            }
        });
    }

    @Override
    public int getItemCount() {
        return entity.getData().getCategories().get(index).getSubcategories() == null ?
                0 : entity.getData().getCategories().get(index).getSubcategories().size();
    }

    public class RightViewHolder extends RecyclerView.ViewHolder {
        LinearLayout ll;
        ImageView iv;
        TextView tv;

        public RightViewHolder(View itemView) {
            super(itemView);
            iv = (ImageView) itemView.findViewById(R.id.iv_right_item);
            tv = (TextView) itemView.findViewById(R.id.tv_right_rvitem);
            ll = (LinearLayout) itemView.findViewById(R.id.ll_item_right);
        }
    }
}
